package com.zoshine.zzg.service;

import com.zoshine.zzg.constant.Session;
import com.zoshine.zzg.dao.*;
import com.zoshine.zzg.exception.AppException;
import com.zoshine.zzg.exception.ErrorMessage;
import com.zoshine.zzg.pojo.*;
import com.zoshine.zzg.pojo.pojostatus.InvoiceStatus;
import com.zoshine.zzg.pojo.pojostatus.OrderSupplierStatus;
import com.zoshine.zzg.pojo.pojostatus.SettlementStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @author 田沛凌
 * @time 2017/7/24 11:01
 */
@Service
@Transactional
public class InvoiceService extends BaseService<Invoice> {
    private InvoiceDao invoiceDao;
    private InvoiceDetailDao invoiceDetailDao;
    private OrderSupplierDao orderSupplierDao;
    private OrderSupplierDetailDao orderSupplierDetailDao;
    private GoodsPublishDetailDao goodsPublishDetailDao;
    private SettlementDao settlementDao;
    private RelationSupplierPrisonerDao relationSupplierPrisonerDao;
    private OrderPrisonerDao orderPrisonerDao;
    private PrisonerInfoDao prisonerInfoDao;
    private ReleaseSlipDao releaseSlipDao;
    private ReleaseSlipDetailDao releaseSlipDetailDao;
    private GoodsHistoryDetailDao goodsHistoryDetailDao;
    @Autowired
    public void setInvoiceDao(InvoiceDao invoiceDao) {
        this.invoiceDao = invoiceDao;
    }

    @Autowired
    public void setInvoiceDetailDao(InvoiceDetailDao invoiceDetailDao) {
        this.invoiceDetailDao = invoiceDetailDao;
    }

    @Autowired
    public void setOrderSupplierDao(OrderSupplierDao orderSupplierDao) {
        this.orderSupplierDao = orderSupplierDao;
    }

    @Autowired
    public void setOrderSupplierDetailDao(OrderSupplierDetailDao orderSupplierDetailDao) {
        this.orderSupplierDetailDao = orderSupplierDetailDao;
    }

    @Autowired
    public void setGoodsPublishDetailDao(GoodsPublishDetailDao goodsPublishDetailDao) {
        this.goodsPublishDetailDao = goodsPublishDetailDao;
    }
    @Autowired
    public void setGoodsHistoryDetailDao(GoodsHistoryDetailDao goodsHistoryDetailDao) {
        this.goodsHistoryDetailDao = goodsHistoryDetailDao;
    }

    @Autowired
    public void setSettlementDao(SettlementDao settlementDao) {
        this.settlementDao = settlementDao;
    }

    @Autowired
    public void setRelationSupplierPrisonerDao(RelationSupplierPrisonerDao relationSupplierPrisonerDao) {
        this.relationSupplierPrisonerDao = relationSupplierPrisonerDao;
    }

    @Autowired
    public void setOrderPrisonerDao(OrderPrisonerDao orderPrisonerDao) {
        this.orderPrisonerDao = orderPrisonerDao;
    }

    @Autowired
    public void setPrisonerInfoDao(PrisonerInfoDao prisonerInfoDao) {
        this.prisonerInfoDao = prisonerInfoDao;
    }

    @Autowired
    public void setReleaseSlipDao(ReleaseSlipDao releaseSlipDao) {
        this.releaseSlipDao = releaseSlipDao;
    }

    @Autowired
    public void setReleaseSlipDetailDao(ReleaseSlipDetailDao releaseSlipDetailDao) {
        this.releaseSlipDetailDao = releaseSlipDetailDao;
    }

    /**
     * 新增发货单
     *
     * @param idAndCounts 待发货单ID和数量;
     */
    public void addInvoice(List<IdAndCount> idAndCounts, HttpSession session) {
        if (null == idAndCounts) {
            throw new AppException(ErrorMessage.REQUEST_PARAM_CAN_NOT_NULL);
        }
        //保存待发货单id和数量，以便生成发货单
        //保存监所账号，根据监所号生成不同的发货单，而改登陆的供应商账户唯一
        List<OrderSupplierDetail> orderSupplierDetailList = new ArrayList<>();
        Set<Long> prisonsIdList = new HashSet<>();
        OrderSupplierDetail tempOrderSupplierDetail=new OrderSupplierDetail();
        for (IdAndCount idAndCount : idAndCounts) {
            //goodsId所指待发货单ID，而不是商品ID
            OrderSupplierDetail orderSupplierDetail = orderSupplierDetailDao.selectByPrimaryKey(idAndCount.getGoodsId());
            orderSupplierDetail.setGoodsCount(idAndCount.getGoodsCount());
            tempOrderSupplierDetail=orderSupplierDetail;
            orderSupplierDetailList.add(orderSupplierDetail);
            prisonsIdList.add(orderSupplierDetail.getAccountPrisons());
        }
        User user = (User) session.getAttribute(Session.LOGIN_USER);
        //根据监所ID生成发货单
        for (Long prisonsId : prisonsIdList) {
            Invoice invoice = new Invoice();
            invoice.setId(baseIdWorker.createId());
            invoice.setAccountSupplier(tempOrderSupplierDetail.getAccountSupplier());
            invoice.setCreateUser(user.getId());
            invoice.setAccountPrisons(prisonsId);
            invoice.setStatus(InvoiceStatus.DELIVERY.getValue());
            invoice.setCreateTime(new Date());
            invoice.setUpdateTime(new Date());
            Integer totalCount = 0;
            Integer totalSell = 0;
            Integer totalSettlement = 0;
            List<InvoiceDetail> invoiceDetailList = new ArrayList<>();
            for (OrderSupplierDetail orderSupplierDetail : orderSupplierDetailList) {
                if (orderSupplierDetail.getAccountPrisons().equals(prisonsId)) {
                    //上架的商品
                    GoodsPublishDetail goodsPublishDetail = goodsPublishDetailDao.selectByPrimaryKey(orderSupplierDetail.getGoodsId());
                    //总和计算
                    totalCount += orderSupplierDetail.getGoodsCount();
                    totalSell += goodsPublishDetail.getGoodsSellPrice() * orderSupplierDetail.getGoodsCount();
                    totalSettlement += goodsPublishDetail.getGoodsSettlementPrice() * orderSupplierDetail.getGoodsCount();
                    //详情
                    InvoiceDetail invoiceDetail = new InvoiceDetail();
                    invoiceDetail.setId(baseIdWorker.createId());
                    invoiceDetail.setInvoiceId(invoice.getId());
                    invoiceDetail.setCount(orderSupplierDetail.getGoodsCount());
                    invoiceDetail.setPublishDetailId(orderSupplierDetail.getGoodsId());
                    invoiceDetail.setGoodsHistoryId(orderSupplierDetail.getGoodsHistoryId());
                    invoiceDetail.setAccountPrisons(orderSupplierDetail.getAccountPrisons());
                    invoiceDetail.setAccountSupplier(orderSupplierDetail.getAccountSupplier());
                    invoiceDetail.setOrderSupplierDetailId(orderSupplierDetail.getId());
                    invoiceDetail.setArea(orderSupplierDetail.getArea());
                    invoiceDetailList.add(invoiceDetail);
                    //更新待发货单(供应商订单详情)的发货数量
                    OrderSupplierDetail temp = orderSupplierDetailDao.selectOne(orderSupplierDetail);
                    temp.setDeliverCount(temp.getDeliverCount() + orderSupplierDetail.getGoodsCount());
                    //判断是否发货超标
                    if (temp.getDeliverCount() > temp.getGoodsCount()) {
                        throw new AppException(ErrorMessage.SEND_GOODS_OUT_OF_BOUNDS);
                    }
                    orderSupplierDetailDao.update(temp);
                    //更新供应商订单状态及发货数量
                    OrderSupplier orderSupplier = orderSupplierDao.selectByPrimaryKey(temp.getOrderSupplierId());
                    orderSupplier.setTotalDeliver(orderSupplier.getTotalDeliver() + orderSupplierDetail.getGoodsCount());
                    //判断是否全部发放完毕
                    if (orderSupplier.getTotalDeliver() < orderSupplier.getTotalCount()) {
                        orderSupplier.setStatus(OrderSupplierStatus.PART_FINISH.getValue());
                    }
                    if (orderSupplier.getTotalDeliver().equals(orderSupplier.getTotalCount())) {
                        orderSupplier.setStatus(OrderSupplierStatus.FINISHED.getValue());
                    }
                    orderSupplierDao.update(orderSupplier);
                }

            }
            invoice.setTotalCount(totalCount);
            invoice.setTotalSell(totalSell);
            invoice.setTotalSettlement(totalSettlement);
            invoiceDao.insert(invoice);
            invoiceDetailDao.batchInsert(invoiceDetailList);
        }


    }

    /**
     * 查询发货单详情集合
     *
     * @param id 发货单号
     * @return 发货单详情
     */
    public List<InvoiceDetail> listInvoiceDetail(Long id) {
        List<InvoiceDetail> listDetails = invoiceDetailDao.selectByInvoiceId(id);
        return listDetails;
    }

    /**
     * 获取单个发货单详情
     *
     * @param id 发货单详情ID
     * @return 发货单详情
     */
    public InvoiceDetail getInvoiceDetailById(Long id) {
        return invoiceDetailDao.selectByPrimaryKey(id);
    }

    /**
     * 批量删除发货单(批量)
     *
     * @param delIdStr 发货单ID集合
     */
    public void delInvoice(String delIdStr) {
        //为空判断
        if (delIdStr == null || delIdStr.length() == 0) {
            throw new AppException(ErrorMessage.REQUEST_PARAM_CAN_NOT_NULL);
        }

        String[] pidArr = delIdStr.split(",");
        for (String id : pidArr) {

            Invoice invoice = invoiceDao.selectByPrimaryKey(Long.parseLong(id));

            if (invoice == null) {
                throw new AppException(ErrorMessage.MODEL_CAN_NOT_FOUND);
            }
            //如果不是待发货状态，则不允许删除
            if (!(invoice.getStatus() == InvoiceStatus.DELIVERY.getValue())) {
                throw new AppException(ErrorMessage.STATUS_SHOULD_BE_DELIVERY);
            }
            List<InvoiceDetail> invoiceDetails = invoiceDetailDao.selectByInvoiceId(invoice.getId());
            for (InvoiceDetail invoiceDetail : invoiceDetails) {
                OrderSupplierDetail orderSupplierDetail = orderSupplierDetailDao.selectByPrimaryKey(invoiceDetail.getOrderSupplierDetailId());
                //totalBackSum+=invoiceDetail.getCount();
                //回改待发货单中的已发货数量
                orderSupplierDetail.setDeliverCount(orderSupplierDetail.getDeliverCount() - invoiceDetail.getCount());
                orderSupplierDetailDao.update(orderSupplierDetail);
                //回改供应商订单的已发货数量
                OrderSupplier orderSupplier = orderSupplierDao.selectByPrimaryKey(orderSupplierDetail.getOrderSupplierId());
                orderSupplier.setTotalDeliver(orderSupplier.getTotalDeliver() - invoiceDetail.getCount());
                //如果发货数量小于0，说明系统出错了
                if (orderSupplier.getTotalDeliver() < 0) {
                    throw new AppException(ErrorMessage.BACK_ERROR);
                }
                if (orderSupplier.getTotalDeliver() == 0) {
                    orderSupplier.setStatus(OrderSupplierStatus.INDELIVERING.getValue());
                }
                if (orderSupplier.getTotalDeliver() < orderSupplier.getTotalCount() && orderSupplier.getTotalDeliver() != 0) {
                    orderSupplier.setStatus(OrderSupplierStatus.PART_FINISH.getValue());
                }
                if (orderSupplier.getTotalDeliver().equals(orderSupplier.getTotalCount())) {
                    orderSupplier.setStatus(OrderSupplierStatus.FINISHED.getValue());
                }
                orderSupplierDao.update(orderSupplier);
            }
            invoiceDao.deleteByPrimaryKey(Long.parseLong(id));
            invoiceDetailDao.delByInvoiceId(Long.parseLong(id));
        }

    }

    /**
     * 删除发货单(单个)
     *
     * @param id 发货单ID
     */
    public void delInvoiceById(Long id) {
        if (id == null) {
            throw new AppException(ErrorMessage.REQUEST_PARAM_CAN_NOT_NULL);
        }
        Invoice invoice = invoiceDao.selectByPrimaryKey(id);
        if (invoice == null) {
            throw new AppException(ErrorMessage.MODEL_CAN_NOT_FOUND);
        }
        //如果不是待发货状态，则不允许删除
        if (!(invoice.getStatus() == InvoiceStatus.DELIVERY.getValue())) {
            throw new AppException(ErrorMessage.STATUS_SHOULD_BE_DELIVERY);
        }
        List<InvoiceDetail> invoiceDetails = invoiceDetailDao.selectByInvoiceId(invoice.getId());
        for (InvoiceDetail invoiceDetail : invoiceDetails) {
            OrderSupplierDetail orderSupplierDetail = orderSupplierDetailDao.selectByPrimaryKey(invoiceDetail.getOrderSupplierDetailId());
            //回改待发货单中的已发货数量
            orderSupplierDetail.setDeliverCount(orderSupplierDetail.getDeliverCount() - invoiceDetail.getCount());
            orderSupplierDetailDao.update(orderSupplierDetail);
            //回改供应商订单的已发货数量
            OrderSupplier orderSupplier = orderSupplierDao.selectByPrimaryKey(orderSupplierDetail.getOrderSupplierId());
            orderSupplier.setTotalDeliver(orderSupplier.getTotalDeliver() - invoiceDetail.getCount());
            //如果发货数量小于0，说明系统出错了
            if (orderSupplier.getTotalDeliver() < 0) {
                throw new AppException(ErrorMessage.BACK_ERROR);
            }
            if (orderSupplier.getTotalDeliver() == 0) {
                orderSupplier.setStatus(OrderSupplierStatus.INDELIVERING.getValue());
            }
            if (orderSupplier.getTotalDeliver() < orderSupplier.getTotalCount() && orderSupplier.getTotalDeliver() != 0) {
                orderSupplier.setStatus(OrderSupplierStatus.PART_FINISH.getValue());
            }
            if (orderSupplier.getTotalDeliver().equals(orderSupplier.getTotalCount())) {
                orderSupplier.setStatus(OrderSupplierStatus.FINISHED.getValue());
            }
            orderSupplierDao.update(orderSupplier);
        }
        invoiceDao.deleteByPrimaryKey(id);
        invoiceDetailDao.delByInvoiceId(id);
    }

    /**
     * 修改发货单详情
     *
     * @param invoiceDetail 发货单详情
     */
    public void editInvoiceDetail(InvoiceDetail invoiceDetail) {

        InvoiceDetail temp = invoiceDetailDao.selectByPrimaryKey(invoiceDetail.getId());
        Invoice invoice = invoiceDao.selectByPrimaryKey(temp.getInvoiceId());
        //如果不是待发货状态，则不允许修改
        if (!(invoice.getStatus() == InvoiceStatus.DELIVERY.getValue())) {
            throw new AppException(ErrorMessage.STATUS_SHOULD_BE_DELIVERY);
        }
        Integer before = temp.getCount();
        Integer after = invoiceDetail.getCount();
         /*商品数量的改变量*/
        Integer sum = after - before;
        GoodsPublishDetail goodsPublishDetail = goodsPublishDetailDao.selectByPrimaryKey(temp.getPublishDetailId());
        Integer totalSell = sum * goodsPublishDetail.getGoodsSellPrice();
        Integer totalSettlement = sum * goodsPublishDetail.getGoodsSettlementPrice();

         /*修改详情*/
        temp.setCount(invoiceDetail.getCount());
        invoiceDetailDao.update(temp);
        /*修改发货单数量及总额*/
        invoice.setTotalCount(invoice.getTotalCount() + sum);
        invoice.setTotalSell(invoice.getTotalSell() + totalSell);
        invoice.setTotalSettlement(invoice.getTotalSettlement() + totalSettlement);
        invoiceDao.update(invoice);
        //修改待发货单信息
        OrderSupplierDetail orderSupplierDetail = orderSupplierDetailDao.selectByPrimaryKey(temp.getOrderSupplierDetailId());
        orderSupplierDetail.setDeliverCount(orderSupplierDetail.getDeliverCount() + sum);
        if (orderSupplierDetail.getDeliverCount() > orderSupplierDetail.getGoodsCount()) {
            throw new AppException(ErrorMessage.SEND_GOODS_OUT_OF_BOUNDS);
        }
        orderSupplierDetailDao.update(orderSupplierDetail);
        //修改供应商订单信息
        OrderSupplier orderSupplier = orderSupplierDao.selectByPrimaryKey(orderSupplierDetail.getOrderSupplierId());
        orderSupplier.setTotalDeliver(orderSupplier.getTotalDeliver() + sum);
        if (orderSupplier.getTotalDeliver().equals(orderSupplier.getTotalCount())) {
            orderSupplier.setStatus(OrderSupplierStatus.FINISHED.getValue());
        }
        if (orderSupplier.getTotalDeliver() < orderSupplier.getTotalCount()) {
            orderSupplier.setStatus(OrderSupplierStatus.PART_FINISH.getValue());
        }
        orderSupplierDao.update(orderSupplier);
    }

    /**
     * 确认发货
     *
     * @param idStr  发货单ID集合
     * @param status 发货单状态
     */
    public void editInvoiceStatus(String idStr, Integer status) {
        //为空判断
        if (idStr == null || idStr.length() == 0) {
            throw new AppException(ErrorMessage.REQUEST_PARAM_CAN_NOT_NULL);
        }

        String[] pidArr = idStr.split(",");
        List<Long> ids = new ArrayList<>();
        for (String id : pidArr) {
            ids.add(Long.parseLong(id));
            //如果已经确认发货则无需再次确认
            Invoice invoice = invoiceDao.selectByPrimaryKey(Long.parseLong(id));
            if (!(invoice.getStatus().equals(InvoiceStatus.DELIVERY.getValue()))) {
                throw new AppException(ErrorMessage.STATUS_SHOULD_BE_DELIVERY);
            }
        }
        invoiceDao.batchUpdateStatus(status, ids);
    }

    /**
     * 确认收货
     *
     * @param idStr  收货单集合ID
     * @param status 收货单状态
     */
    public void confirmInvoice(String idStr, Integer status) {
        //为空判断
        if (idStr == null || idStr.length() == 0) {
            throw new AppException(ErrorMessage.REQUEST_PARAM_CAN_NOT_NULL);
        }

        String[] pidArr = idStr.split(",");
        List<Long> ids = new ArrayList<>();
        for (String id : pidArr) {
            ids.add(Long.parseLong(id));
            //判断是否是已导入内网状态
            Invoice invoice = invoiceDao.selectByPrimaryKey(Long.parseLong(id));
            if (!(invoice.getStatus().equals(InvoiceStatus.PENDING_AUDIT.getValue()))) {
                throw new AppException(ErrorMessage.STATUS_SHOULD_BE_FINISH);
            }
        }
        invoiceDao.batchUpdateStatus(status, ids);
    }

    /**
     * 生成结算单
     *
     * @param invoices 发货单集合
     * @param status   发货单状态
     */
    public void settlement(List<Invoice> invoices, Integer status) {
        List<Long> ids = new ArrayList<>();
        Set<Long> supplierIds = new HashSet<>();
        for (Invoice temp : invoices) {
            ids.add(temp.getId());
            supplierIds.add(temp.getAccountSupplier());
        }
        //根据供应商生成结算单
        for (Long supplierId : supplierIds) {
            Integer totalSettlement = 0;
            //生成结算单
            Settlement settlement = new Settlement();
            settlement.setId(baseIdWorker.createId());
            //此发货单全是同一监所下的发货单
            Invoice temp = null;
            for (Invoice invoice : invoices) {
                if (invoice.getAccountSupplier().equals(supplierId)) {
                    temp = invoice;
                    totalSettlement += invoice.getTotalSettlement();
                    //更新发货单，添加结算书字段
                    invoice.setSettlementId(settlement.getId());
                    invoice.setUpdateTime(new Date());
                    invoiceDao.update(invoice);
                }
            }
            if (temp != null) {
                settlement.setAccountPrisons(temp.getAccountPrisons());
                settlement.setCreateUser(temp.getCreateUser());
            }
            settlement.setAccountSupplier(supplierId);
            settlement.setTotalSettlement(totalSettlement);
            settlement.setUpdateTime(new Date());
            settlement.setCreateTime(new Date());
            settlement.setStatus(SettlementStatus.PENDING_RECEIVE.getValue());
            settlementDao.insert(settlement);
        }
        //更新发货单状态
        invoiceDao.batchUpdateStatus(status, ids);

    }

    /**
     * 删除发货单详情
     *
     * @param id 发货单详情ID
     */
    public void delInvoiceDetail(Long id) {
        InvoiceDetail invoiceDetail = invoiceDetailDao.selectByPrimaryKey(id);
        Invoice invoice = invoiceDao.selectByPrimaryKey(invoiceDetail.getInvoiceId());
        //如果不是待发货状态，则不允许删除
        if (!(invoice.getStatus() == InvoiceStatus.DELIVERY.getValue())) {
            throw new AppException(ErrorMessage.STATUS_SHOULD_BE_DELIVERY);
        }
        invoice.setTotalCount(invoice.getTotalCount() - invoiceDetail.getCount());
        if (invoice.getTotalCount() < 0) {
            throw new AppException(ErrorMessage.BACK_ERROR);
        }
        //修改代发货数量
        OrderSupplierDetail orderSupplierDetail = orderSupplierDetailDao.selectByPrimaryKey(invoiceDetail.getOrderSupplierDetailId());
        orderSupplierDetail.setDeliverCount(orderSupplierDetail.getDeliverCount() - invoiceDetail.getCount());
        //如果数量小于0，说明系统出错了
        if (orderSupplierDetail.getDeliverCount() < 0) {
            throw new AppException(ErrorMessage.BACK_ERROR);
        }
        orderSupplierDetailDao.update(orderSupplierDetail);
        //修改供应商订单数量
        OrderSupplier orderSupplier = orderSupplierDao.selectByPrimaryKey(orderSupplierDetail.getOrderSupplierId());
        orderSupplier.setTotalDeliver(orderSupplier.getTotalDeliver() - invoiceDetail.getCount());
        if (orderSupplier.getTotalDeliver() < 0) {
            throw new AppException(ErrorMessage.BACK_ERROR);
        }
        if (orderSupplier.getTotalDeliver() == 0) {
            orderSupplier.setStatus(OrderSupplierStatus.INDELIVERING.getValue());
        }
        if (orderSupplier.getTotalDeliver() < orderSupplier.getTotalCount() && orderSupplier.getTotalDeliver() != 0) {
            orderSupplier.setStatus(OrderSupplierStatus.PART_FINISH.getValue());
        }
        if (orderSupplier.getTotalDeliver().equals(orderSupplier.getTotalCount())) {
            orderSupplier.setStatus(OrderSupplierStatus.FINISHED.getValue());
        }
        orderSupplierDao.update(orderSupplier);
        //修改发货单数量
        invoiceDao.update(invoice);
        //删除发货单详情
        invoiceDetailDao.deleteByPrimaryKey(id);
        //如果发货单没有详情，则发货单删除
        List<InvoiceDetail> invoiceDetails = invoiceDetailDao.selectByInvoiceId(invoice.getId());
        if (invoiceDetails.size() == 0) {
            invoiceDao.deleteByPrimaryKey(invoice.getId());
        }
    }

    /**
     * 缺货登记犯人信息查询
     *
     * @param invoiceId;
     * @return 犯人信息集合
     */
    public List<PrisonerInfo> getPrisonerInfoByInvoiceId(Long invoiceId) {

        //根据发货单号查询发货单详情
        List<InvoiceDetail> invoiceDetails = invoiceDetailDao.selectByInvoiceId(invoiceId);

        //获取供应商订单详情ID
        List<Long> orderSupplierDetails = new ArrayList<>();
        for (InvoiceDetail invoiceDetail : invoiceDetails) {
            orderSupplierDetails.add(invoiceDetail.getOrderSupplierDetailId());
        }
        //获取非重复的供应商订单ID
        Set<Long> supplierIds = new HashSet<>();
        for (Long orderSupplierDetail : orderSupplierDetails) {
            OrderSupplierDetail orderSupplierDetail1 = orderSupplierDetailDao.selectByPrimaryKey(orderSupplierDetail);
            supplierIds.add(orderSupplierDetail1.getOrderSupplierId());
        }
        //根据供应商ID获取犯人点单信息ID
        Set<Long> orderPrisonerIds = new HashSet<>();
        for (Long supplierId : supplierIds) {
            List<RelationSupplierPrisoner> relationSupplierPrisoners = relationSupplierPrisonerDao.selectByOrderSupplierId(supplierId);
            for (RelationSupplierPrisoner relationSupplierPrisoner : relationSupplierPrisoners) {
                Long orderPrisonerId = relationSupplierPrisoner.getOrderPrisonerId();
                orderPrisonerIds.add(orderPrisonerId);
            }
        }
        //根据犯人点单ID获取犯人ID
        Set<Long> prisonerIds = new HashSet<>();
        for (Long orderPrisonerId : orderPrisonerIds) {
            OrderPrisoner orderPrisoner = orderPrisonerDao.selectByPrimaryKey(orderPrisonerId);
            prisonerIds.add(orderPrisoner.getPrisonerInfo());
        }
        //查询犯人集合
        List<PrisonerInfo> prisonerInfos = new ArrayList<>();
        for (Long prisonerId : prisonerIds) {
            PrisonerInfo prisonerInfo = prisonerInfoDao.selectByPrimaryKey(prisonerId);
            prisonerInfos.add(prisonerInfo);
        }
        return prisonerInfos;
    }

    /**
     * Register for out of stock
     * 根据犯人ID和发货单ID获取商品信息
     *
     * @param invoiceId  发货单ID
     * @param prisonerId 犯人ID
     * @return 商品信息
     */
    public List<GoodsHistoryDetail> getGoodsHistoryDetail(Long invoiceId, Long prisonerId) {
        //根据犯人ID查询发放单
        if (invoiceId == null) {
            throw new AppException(ErrorMessage.ORDER_RECEIVE_ID_CAN_NOT_NULL);
        }
        if (prisonerId == null) {
            throw new AppException(ErrorMessage.PRISONER_ID_CAN_NOT_NULL);
        }
        ReleaseSlip releaseSlip = releaseSlipDao.selectByInvoiceId(invoiceId);
        ReleaseSlipDetail releaseSlipDetail = new ReleaseSlipDetail();
        releaseSlipDetail.setReleaseSlipId(releaseSlip.getId());
        List<ReleaseSlipDetail> releaseSlipDetails = releaseSlipDetailDao.selectList(releaseSlipDetail);
        //获取某个犯人商品信息
        List<GoodsHistoryDetail> goodsHistoryDetails = new ArrayList<>();
        for (ReleaseSlipDetail releaseSlipDetail1 : releaseSlipDetails) {
            if (releaseSlipDetail1.getPrisonerId().equals(prisonerId)) {
                GoodsHistoryDetail goodsHistoryDetail = goodsHistoryDetailDao.selectByPrimaryKey(releaseSlipDetail1.getGoodsHistoryId());
                goodsHistoryDetails.add(goodsHistoryDetail);
            }
        }
        System.out.println(goodsHistoryDetails.size());
        return goodsHistoryDetails;
    }
}
